package com.jiwei.system.serviceImpl;

import com.jiwei.system.dao.ItemMapper;
import com.jiwei.system.pojo.Book;
import com.jiwei.system.pojo.Borrow;
import com.jiwei.system.pojo.Things;
import com.jiwei.system.service.ItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ItemService")
public class ItemServiceImpl implements ItemService {

    @Resource
    private ItemMapper itemMapper;

    @Override
    public List<Borrow> getMemberSelfBorrowingItemList(String account) {

        return itemMapper.getMemberBorrowItemListByAccount(account);
    }

    /**
     * 获取借用物品的数量
     * @param page 页数
     * @param limit 每次返回数量
     * @return
     */
    @Override
    public List<Things> getThingList(int page, int limit) {
        int start = (page - 1) * limit;
        return itemMapper.getThingsList(start,limit);
    }


    public int getThingListNum() {
        return itemMapper.queryThingNum();
    }

    @Override
    public List<Book> getBookList(int page, int limit) {
        int start = (page - 1) * limit;
        return itemMapper.getBookList(start,limit);
    }


    public int getBookListNum() {
        return itemMapper.queryBookNum();
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void borrowThing(Borrow borrow) {
        int currentAmount = itemMapper.getThingCurrentCountByTid(borrow.getTid());
        if(currentAmount!=0){
            System.out.print("借用前数量：");
            System.out.println(currentAmount);
            borrow.setStatus("未归还");
            itemMapper.insertBorrowItem(borrow);
            currentAmount = currentAmount - borrow.getAmount();
            System.out.print("借用后数量：");
            System.out.println(currentAmount);
            Map<String, Object> attribute = new HashMap<>();
            attribute.put("tid",borrow.getTid());
            attribute.put("currentAmount",currentAmount);
            itemMapper.updateCurrentCountByTid(attribute);
        }

    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void returnThing(Borrow borrow) {
        if(itemMapper.getStatusById(borrow.getId()).equals("未归还")){
            int currentAmount = itemMapper.getThingCurrentCountByTid(borrow.getTid());
            System.out.print("归还前数量：");
            System.out.println(currentAmount);
            borrow.setStatus("已归还");
            if(itemMapper.updateBorrowItem(borrow)){    //归还成功更新borrow再更新数量
                currentAmount = currentAmount + borrow.getAmount();
                Map<String, Object> attribute = new HashMap<>();
                attribute.put("tid",borrow.getTid());
                attribute.put("currentAmount",currentAmount);
                itemMapper.updateCurrentCountByTid(attribute);
                System.out.print("归还后数量：");
                System.out.println(currentAmount);
            }

        }

    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void borrowBook(Borrow borrow) {
        int currentAmount = itemMapper.getBookCurrentCountByBid(borrow.getBid());
        System.out.println(borrow.getBid());
        if(currentAmount!=0){
            System.out.print("借用前数量：");
            System.out.println(currentAmount);
            borrow.setStatus("未归还");
            itemMapper.insertBorrowItem(borrow);
            currentAmount = currentAmount - borrow.getAmount();
            System.out.print("借用后数量：");
            System.out.println(currentAmount);
            Map<String, Object> attribute = new HashMap<>();
            attribute.put("bid",borrow.getBid());
            attribute.put("currentAmount",currentAmount);
            itemMapper.updateCurrentCountByBid(attribute);
        }
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void returnBook(Borrow borrow) {
        if(itemMapper.getStatusById(borrow.getId()).equals("未归还")){
            int currentAmount = itemMapper.getBookCurrentCountByBid(borrow.getBid());
            System.out.print("归还前数量：");
            System.out.println(currentAmount);
            borrow.setStatus("已归还");
            if(itemMapper.updateBorrowItem(borrow)){
                currentAmount = currentAmount + borrow.getAmount();
                Map<String, Object> attribute = new HashMap<>();
                attribute.put("bid",borrow.getBid());
                attribute.put("currentAmount",currentAmount);
                itemMapper.updateCurrentCountByBid(attribute);
                System.out.print("归还后数量：");
                System.out.println(currentAmount);
            }

        }
    }

    @Override
    public List<Borrow> getBorrowHistoryList(int page, int limit) {
        int start = (page - 1) * limit;
        return itemMapper.getBorrowHistoryList(start,limit);
    }


    public int getBorrowHistoryListNum() {
        return itemMapper.queryBorrowNum();
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void addNewThing(Things things) {
        itemMapper.addNewThing(things);
    }

    @Override
    public void addNewBook(Book book) {
        itemMapper.addNewBook(book);
    }

    @Override
    public void modifyThing(Things things) {itemMapper.modifyThing(things);}

    @Override
    public void modifyBook(Book book) {itemMapper.modifyBook(book);}


}
